package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;

import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.Random;

/**
 * 绘制默认背景图
 */
public class X3ModifyHeaderView {

    /**
     * 图片的宽高
     */
    private static int mWidth;

    /**
     * 暂时处理中文
     *
     * @param
     */
    public static String setName(String lastName,String firstName, Context mContext) {
        mWidth = X3ScreenUtils.dipToPx(88, mContext);

        if (X3StringUtils.isEmpty(lastName)) {
            /**
             * 名字为空 那么直接绘制就完了
             */
            return getDefaultHeader("", mContext);
        }

        boolean chinaAndTw = X3Constent.isChinaAndTw();
        if (chinaAndTw) {
            if (lastName.length() > 0) {
                return getDefaultHeader(lastName.substring(0, 1), mContext);
            }else {
                return getDefaultHeader("", mContext);
            }
        }else {
            if (!X3StringUtils.isEmpty(firstName)) {
                return getDefaultHeader(firstName.substring(0, 1) + lastName.substring(0,1), mContext);
            }else {
                if (lastName.length() > 1) {
                    return getDefaultHeader(lastName.substring(0, 2), mContext);
                }else {
                    return getDefaultHeader(lastName.substring(0, 1), mContext);
                }
            }
        }
    }

    private static String getDefaultHeader(String name, Context mContext) {
        String[] colorList = new String[]{"#0077FF","#5FADF3","#5952BB","#843952","#ea4c89","#F2292B","#FFC89B","#4C09C1","#FEA701","#9f7563","#00CB00","#A2CB00","#50E3C2","#9FA6C7"};
        /**
         * 生成矩形头像
         */
        Bitmap rectBitmap = Bitmap.createBitmap(mWidth, mWidth, Bitmap.Config.ARGB_8888);
        /**
         * 绘制头像背景
         */
        Canvas rectCanvas = new Canvas(rectBitmap);

        rectCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        Rect rect = new Rect(0, 0, rectBitmap.getWidth(), rectBitmap.getHeight());
        RectF rectF = new RectF(rect);

        Paint rectPaint = new Paint();
        rectPaint.setAntiAlias(true);

        Random random = new Random();

        rectPaint.setColor(Color.parseColor(colorList[random.nextInt(colorList.length - 1)]));
        rectCanvas.drawRect(rectF, rectPaint);

        rectPaint.setColor(ContextCompat.getColor(mContext, R.color.cff));
        rectPaint.setTextSize(X3ScreenUtils.sp2px(30, mContext));
        rectPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = rectPaint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = rectF.centerY() + distance;
        rectCanvas.drawText(name, rectF.centerX(), baseline, rectPaint);

        return X3FileUtils.saveBitmap(mContext, rectBitmap);
    }

}
